package com.exam.master.model.main.view;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.bean.MessageInfo;
import com.exam.commonbiz.event.NewMessageEvent;
import com.exam.master.R;
import com.exam.master.databinding.ActivityMessageDetailBinding;
import com.exam.commonbiz.model.IMessageDetailCallBack;
import com.exam.commonbiz.viewmodel.CommonViewModel;
import com.exam.master.model.main.adapter.MessageDetailAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class MessageDetailActivity extends BaseActivity<CommonViewModel, ActivityMessageDetailBinding> implements IMessageDetailCallBack {

    private String conversationId;
    private String nickname;
    private MessageDetailAdapter mMessageDetailAdapter;

    public static void start(Context context, String nickname, String conversationId) {
        Intent starter = new Intent(context, MessageDetailActivity.class);
        starter.putExtra("nickname", nickname);
        starter.putExtra("conversationId", conversationId);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message_detail;
    }

    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    /**
     * 更改状态栏颜色，只有非全屏模式下有效
     */
    public int getStatusBarColor() {
        return R.color.color_bg;
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Override
    public void initData() {

        conversationId = getIntent().getStringExtra("conversationId");
        nickname = getIntent().getStringExtra("nickname");
        binding.includeTitle.tvTitle.setText(nickname + "");
        binding.includeTitle.ivback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mMessageDetailAdapter = new MessageDetailAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        linearLayoutManager.setStackFromEnd(true);
        linearLayoutManager.setReverseLayout(true);
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        binding.recyclerView.setAdapter(mMessageDetailAdapter);
        binding.recyclerView.setNestedScrollingEnabled(true);

        binding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        mViewModel.setIMessageDetailCallBack(this);
        binding.swipeRefreshLayout.setColorSchemeResources(R.color.main_color);
        binding.recyclerView.setNestedScrollingEnabled(true);

        getData();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void getData() {
        if (mViewModel != null) {
            mViewModel.getMessageDetailList(conversationId);
        }
    }

    @Override
    public void returnMessageDetailList(List<MessageInfo> messageInfoList) {
        binding.swipeRefreshLayout.setRefreshing(false);
        if (messageInfoList == null || messageInfoList.size() == 0) {
            mMessageDetailAdapter.loadMoreEnd();
            return;
        }
        mMessageDetailAdapter.setNewData(messageInfoList);
        mMessageDetailAdapter.loadMoreComplete();
        binding.recyclerView.scrollToPosition(0);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNewMessageEvent(NewMessageEvent newMessageEvent) {
        getData();
    }
}